package toolkits;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


public class XMLHandle {
    
    static{
        //System.out.println("Load Class!!!!!!!!!!!!!!!!!!!!"+Thread.currentThread().getContextClassLoader());
    }
    
    public static void main(String[] args) throws Exception {
        
        xmltest();
    }
    
    private static void xmltest()throws Exception{
        SAXReader xmlReader = new SAXReader();
        File file = new File("E:\\workspace3.4\\C7pcisecureorderform2\\src\\site\\paymentserversettings\\avetti.xml");
        System.out.println("!!!!! "+file.getName());
        System.out.println("!!!!! "+file);
        Document merchantidDoc = xmlReader.read(file);
        org.dom4j.Element root=merchantidDoc.getRootElement();
        System.out.println("===== xxx "+root.element("vxxx"));
        Node node = merchantidDoc.selectSingleNode( "//merchant/v20070723002" );
        System.out.println("===== node "+node);
        for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
            Element ve = (Element) i.next();
            System.out.println("===== "+ve.asXML());
            //System.out.println(element.getText());
            
            for(Iterator j = ve.elementIterator(); j.hasNext();){
                Element ce = (Element) j.next();
                System.out.println("Att===== "+ce.attributeValue("vid"));
                for ( Iterator ai = ce.attributeIterator(); ai.hasNext(); ) {
                    Attribute attribute = (Attribute) ai.next();
                    //System.out.println("Att===== "+attribute.getValue());
                }
                
                for(Iterator k = ce.elementIterator("delete"); k.hasNext();){
                    Element de = (Element) k.next();
                    ce.remove(de);
                    System.out.println("##### remove "+de.getName() + " " + de.getText());
                }
                
                System.out.println("-->"+ce.elementText("primaryuserid")+"<--");
                
                for(Iterator k = ce.elementIterator("primarypassword"); k.hasNext();){
                    Element te = (Element) k.next();
                    System.out.println("##### "+te.getText());
                }
            }
        }
        
        
        write(merchantidDoc);
    }
    
    public static void write(Document document) throws IOException {

        // lets write to a file
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(new FileWriter( "E:\\workspace3.4\\C7pcisecureorderform2\\src\\site\\paymentserversettings\\demo.xml" )
                                    ,format);
        writer.write( document );
        writer.close();
        
        /*
        // Pretty print the document to System.out
        OutputFormat format = OutputFormat.createPrettyPrint();
        writer = new XMLWriter( System.out, format );
        writer.write( document );

        // Compact format to System.out
        format = OutputFormat.createCompactFormat();
        writer = new XMLWriter( System.out, format );
        writer.write( document );
        */
    }

}
